package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.XinWenDaoimpl;
import com.ysd.entity.XinWen;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminXinWenFenLeiServlet extends BaseServlet {
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=selXinWenFenLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selXinWenFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		List<XinWen> xinWen=xinWenDao.getXinWen();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(xinWen));
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=selXinWenFenLeiByPage
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selXinWenFenLeiByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		List<XinWen> xinWen=xinWenDao.getXinWenByPage(page,size);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(xinWen));
		
		out.flush();
		out.close();
	}
	
	/**
	 *  localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=delXinWenFenLei
	 *  通过id删除数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delXinWenFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		int xinWenId=Integer.parseInt(request.getParameter("xinWenId"));	
		int n=xinWenDao.delXinWenFenLei(xinWenId);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(false, "删除失败", "您的操作有误"));			
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=addXinWenFenLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addXinWenFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		String xinWenNameFenLei=request.getParameter("xinWenNameFenLei");
		
		if(xinWenNameFenLei !=null && !xinWenNameFenLei.equals("")) {
			ArrayList<XinWen> xwList=xinWenDao.selXinWenFenLeiByName(xinWenNameFenLei);
			if(xwList.size()==0) {
				XinWen xw=new XinWen(0, xinWenNameFenLei);
				int n=xinWenDao.addXinWenFenLei(xw);
				
				
				if(n>0) {
					out.print(Result.toClient(true, "添加成功", ""));
				}else {
					out.print(Result.toClient(false, "添加失败", "您的操作有误"));			
				}
			}else {
				out.print(Result.toClient(false, "已存在此分类", "您的操作有误"));			
			}
		}
		
		
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=getXinwenFenLeiById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getXinwenFenLeiById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		String xwflid=request.getParameter("xinWenFenLeiId");
		int xinWenFenLeiId=0;		
		if(xwflid!=null && !xwflid.equals("")) {
			xinWenFenLeiId=Integer.parseInt(xwflid);
		}
		XinWen xinWen=xinWenDao.getXinwenFenLeiById(xinWenFenLeiId);
		
		
		Gson gson=new Gson();
		out.print(gson.toJson(xinWen));
		
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=editXinWenFenLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editXinWenFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		
		String xinWenNameFenLei=request.getParameter("xinWenFenLeiName");
		String xwflid=request.getParameter("xinWenFenLeiId");
		int xinWenFenLeiId=0;		
		if(xwflid!=null && !xwflid.equals("")) {
			xinWenFenLeiId=Integer.parseInt(xwflid);
		}

		
		if(xinWenNameFenLei !=null && !xinWenNameFenLei.equals("")) {
			ArrayList<XinWen> xwList=xinWenDao.selXinWenFenLeiByName(xinWenNameFenLei);
			if(xwList.size()==0) {
				XinWen xw=new XinWen(xinWenFenLeiId, xinWenNameFenLei);
				int n=xinWenDao.editXinWenFenLei(xw);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功", ""));
				}else {
					out.print(Result.toClient(false, "修改失败", "您的操作有误"));			
				}
			}else {
				out.print(Result.toClient(false, "此分类名已存在", "请重新修改"));			
			}
		}
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenfenlei?reqName=selXinWenFenLeiBysize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selXinWenFenLeiBysize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XinWenDaoimpl xinWenDao=new XinWenDaoimpl();
		ArrayList<XinWen> xinWen=xinWenDao.getXinwenFenLeiSize();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(xinWen.size()));
		
		out.flush();
		out.close();
	}
	
}
